package com.bcinfo.iccard.server;

import java.io.IOException;
import java.net.InetSocketAddress;

import org.apache.commons.lang3.exception.ExceptionUtils;
import org.apache.log4j.Logger;
import org.apache.mina.core.service.IoAcceptor;

public class MinaService {
	private static final Logger _logger=Logger.getLogger(MinaService.class);
	private IoAcceptor acceptor;
	private int port;

	public int getPort() {
		return port;
	}

	public void setPort(int port) {
		this.port = port;
	}

	public IoAcceptor getAcceptor() {
		return acceptor;
	}

	public void setAcceptor(IoAcceptor acceptor) {
		this.acceptor = acceptor;
	}
	public void start(){
		if(acceptor!=null){
			acceptor.setDefaultLocalAddress(new InetSocketAddress(getPort()));  
	        try {
				acceptor.bind();
			} catch (IOException e) {
				_logger.error("start Mina Server failed!");
				_logger.error(ExceptionUtils.getStackTrace(e));
			} //启动监听  
	        _logger.info("Mina Server started, Listen on:"+getPort());  
		}else{
			_logger.error("IoAcceptor initial error, failed to start Mina Server!");
		}
	}
	
	public void stop(){
		if(acceptor!=null){
			acceptor.unbind();
			_logger.info("Mina Server has been stopped!");
		}
	}
}
